package com.tnxst.phexamination.public_health_docking_interface;

import com.tnxst.phexamination.constant.Trade;
import com.tnxst.phexamination.dto.TbFirstFollowDTO;
import com.tnxst.phexamination.dto.TbFollowDTO;
import com.tnxst.phexamination.dto.TbPatientRecordDTO;
import com.tnxst.phexamination.utils.ReflectUtils;
import com.tnxst.phexamination.utils.StringUtils;
import com.tnxst.phexamination.utils.WebServiceUtils;

import java.util.LinkedHashMap;
import java.util.Map;

public class TuberculosisInterface {

    /**
     * 67-2 增加或修改结核病患者档案
     * @param tbPatientRecordDTO
     * @param response
     */
    public static void saveOrUpdateTbPatientRecord(TbPatientRecordDTO tbPatientRecordDTO, String OrgId, WebServiceUtils.Response<Map> response){
        String trade = Trade.TUBERCULOSIS.SAVE_OR_UPDATE_TB_PATIENT_RECORD;
        Map<String, Object> data = StringUtils.getDataMap();
        data.put("TbId",tbPatientRecordDTO.getTbId());
        LinkedHashMap<String, Object> cmPerson = new LinkedHashMap<>();
        cmPerson.put("OrgID",OrgId);
        LinkedHashMap<String, Object> tbBaseInfo = new LinkedHashMap<>();
        data.put("CmPerson",cmPerson);
        data.put("TbBaseInfo",tbBaseInfo);
        fillTbPatientRecord(tbPatientRecordDTO,cmPerson,tbBaseInfo);
        WebServiceUtils.call(trade,data,response);

    }

    private static void fillTbPatientRecord(TbPatientRecordDTO tbPatientRecordDTO, LinkedHashMap<String, Object> cmPerson, LinkedHashMap<String, Object> tbBaseInfo){
        String[] cmPersonNames = {"DiagnosisDate","DoctorID","DoctorName","PersonID","Remark"};
        String[] tbBaseInfoNames = {"ChemotherapyRegimen","CmWay","DiagnosticTyping","InitialTreatment","PaymentMethod",
                "ReTreatment","RecDate","RecNo","RecentTreatmentDate","Status","TreatmentDate","TreatmentOrgType"};
        ReflectUtils.objectToMap(cmPersonNames,tbPatientRecordDTO,cmPerson);
        ReflectUtils.objectToMap(tbBaseInfoNames,tbPatientRecordDTO,tbBaseInfo);
    }

    /**
     * 67-5 增加或修改结核病患者首次随访记录
     * @param tbFirstFollowDTO
     * @param response
     */
    public static void saveOrUpdateTbFirstFollow(TbFirstFollowDTO tbFirstFollowDTO, String personId,
                                                 String orgId, WebServiceUtils.Response response){
        String trade = Trade.TUBERCULOSIS.SAVE_OR_UPDATE_TB_FIRST_FOLLOW;
        Map<String, Object> data = StringUtils.getDataMap();
        LinkedHashMap<String, Object> tbFirstVisit = new LinkedHashMap<>();
        LinkedHashMap<String, Object> tbDrugUse = new LinkedHashMap<>();
        LinkedHashMap<String, Object> examLifestyle = new LinkedHashMap<>();
        tbFirstVisit.put("PersonID",personId);
        tbFirstVisit.put("OrgID",orgId);
        data.put("tbFirstVisit",tbFirstVisit);
        data.put("tbDrugUse",tbDrugUse);
        data.put("examLifestyle",examLifestyle);
        data.put("otherJson",tbFirstFollowDTO.getOtherJson());
        fillTbFirstFollow(tbFirstFollowDTO,tbFirstVisit,tbDrugUse,examLifestyle);
        WebServiceUtils.call(trade,data,response);

    }

    /**
     * 67-6 结核病首次随访记录
     * @param tbBaseId
     * @param response
     */
            public static void queryTbFirstFollow(String tbBaseId,  WebServiceUtils.Response response){
        String trade = Trade.TUBERCULOSIS.QUERY_TB_FIRST_FOLLOW;
        Map<String, Object> data = StringUtils.getDataMap();

//        Map<String, Object> tbFirstVisit = new LinkedHashMap<>();
//        tbFirstVisit.put("PersonID",personId);
//        tbFirstVisit.put("OrgID",orgId);
        data.put("TbBaseId",tbBaseId);
//        data.put("tbFirstVisit",tbFirstVisit);
        WebServiceUtils.call(trade,data,response);
    }

    /**
     * 67-14 新结核病随访列表
     * @param pageSize
     * @param pageIndex
     * @param personId
     * @param response
     */
    public static void queryNewTbFollowList(Integer pageSize, Integer pageIndex, String personId,
                                            WebServiceUtils.Response response){
        String trade = Trade.TUBERCULOSIS.QUERY_NEW_TB_FOLLOW_LIST;
        Map<String, Object> data = StringUtils.getPageDataMap(pageSize,pageIndex);
        data.put("personId",personId);
        WebServiceUtils.call(trade,data,response);
    }


    /**
     * 67-15 删除新结核随访记录
     * @param id
     * @param tbBaseID
     * @param response
     */
    public static void deleteNewTbFollow(String id, String tbBaseID,
                                            WebServiceUtils.Response response){
        String trade = Trade.TUBERCULOSIS.DELETE_NEW_TB_FOLLOW;
        Map<String, Object> data = StringUtils.getDataMap();
        data.put("id",id);
        data.put("tbBaseID",tbBaseID);
        WebServiceUtils.call(trade,data,response);

    }

    /**
     * 67-16 获取患者个人信息
     * @param personId
     * @param response
     */
    public static void getPersonalPatientInfo(String personId, WebServiceUtils.Response response){
        String trade = Trade.TUBERCULOSIS.GET_PERSONAL_PATIENT_INFO;
        Map<String, Object> data = StringUtils.getDataMap();
        data.put("id",personId);
        WebServiceUtils.call(trade,data,response);
    }



    /**
     * 67-17 验证居民是否是结核病患者
     * @param personId
     * @param response
     */
    public static void verifyIsTbPatient( String personId, WebServiceUtils.Response response){
        String trade = Trade.TUBERCULOSIS.VERIFY_IS_TB_PATIENT;
        Map<String, Object> data = StringUtils.getDataMap();
        data.put("personId",personId);
        WebServiceUtils.call(trade,data,response);
    }


    /**
     *  67-18 获取新结核随访记录
     * @param personId
     * @param id
     * @param response
     */
    public static void queryNewTbFollow(String personId, String id, WebServiceUtils.Response response){
        String trade = Trade.TUBERCULOSIS.QUERY_NEW_TB_FOLLOW;
        Map<String, Object> data = StringUtils.getDataMap();
        data.put("personID",personId);
        data.put("id",id);
        WebServiceUtils.call(trade,data,response);
    }


    /**
     * 67-19 新增或更新新结核随访记录
     * @param tbFollowDTO
     * @param response
     */
    public static void saveOrUpdateTbFollow(TbFollowDTO tbFollowDTO, WebServiceUtils.Response response){
        String trade = Trade.TUBERCULOSIS.SAVE_UPDATE_TB_FOLLOW;
        Map<String, Object> data = StringUtils.getDataMap();
        LinkedHashMap<String, Object> examLifestyle = new LinkedHashMap<>();
        LinkedHashMap<String, Object> tbDrugUse = new LinkedHashMap<>();
        LinkedHashMap<String, Object> tbFollowUp2 = new LinkedHashMap<>();
        data.put("examLifestyle",examLifestyle);
        data.put("tbDrugUse",tbDrugUse);
        data.put("otherText",tbFollowDTO.getOtherText());
        data.put("tbFollowUp2",tbFollowUp2);
        fillTbFollow(tbFollowDTO,examLifestyle,tbDrugUse,tbFollowUp2);
        WebServiceUtils.call(trade,data,response);
    }

    private static void fillTbFollow(TbFollowDTO tbFollowDTO, LinkedHashMap<String, Object> examLifestyle,
                                     LinkedHashMap<String, Object> tbDrugUse, LinkedHashMap<String, Object> tbFollowUp2){
        String[] examLifestyleNames = {"DailyAlcoholIntake","Smoking"};
        String[] tbDrugUseNames = {"ChemotherapyRegimen","Dosage","MissedMedNum","Usage"};
        String[] tbFollowUp2Names = {"AdverseDrugReactions","Complication","DoctorID",
                "DoctorName","FollowUpDate","FollowUpRemarks","ID","NextDailyAlcohol",
                "NextFollowUpDate","NextSmoking","PersonID","Supervisor","Symptom","TbBaseID",
                "TranOut","TreatmentMonthOrder","WayUp","StopTreatDate","StopTreatReason","ShouldFollowUpCnt",
                "ActualFollowUpCnt","ShouldMedicationCnt","ActualMedicationCnt","MedicationRate","AppraiseDoctorID","AppraiseDoctorName"};
        ReflectUtils.objectToMap(examLifestyleNames,tbFollowDTO,examLifestyle);
        ReflectUtils.objectToMap(tbDrugUseNames,tbFollowDTO,tbDrugUse);
        ReflectUtils.objectToMap(tbFollowUp2Names,tbFollowDTO,tbFollowUp2);
    }

    private static void fillTbFirstFollow(TbFirstFollowDTO tbFirstFollowDTO, Map tbFirstVisit, Map tbDrugUse, Map examLifestyle){
        String[] tbFirstVisitNames = {"AdverseDrugReactions","AlongBedroom","AreationType","ContactsExam","DoctorID",
                "DoctorName","DrugCardFill","DrugCardStore","DrugFastType","DrugHarm","FollowUpDate","ID",
                "LivingPrecautions","NextDailyAlcohol","NextFollowUpDate","NextSmoking","OutdoorMedication",
                "PatientType","PecipeDate","RecipePlace","SputumType","Supervisor","Symptom","TbBaseID",
                "TbTreatment","TreatementSputum","WayUp"};
        ReflectUtils.objectToMap(tbFirstVisitNames,tbFirstFollowDTO,tbFirstVisit);
        String[] tbDrugUseNames = {"ChemotherapyRegimen","Dosage","Usage"};
        ReflectUtils.objectToMap(tbDrugUseNames,tbFirstFollowDTO,tbDrugUse);
        String[] examLifestyleNames = {"DailyAlcoholIntake","Smoking"};
        ReflectUtils.objectToMap(examLifestyleNames,tbFirstFollowDTO,examLifestyle);
    }




}
